package com.zyj.assets.service.impl;

import java.util.List;

import com.zyj.common.core.text.Convert;
import com.zyj.common.utils.DateUtils;
import com.zyj.common.utils.StringUtils;
import com.zyj.receive.domain.AssetsReceive;
import com.zyj.receive.mapper.AssetsReceiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.zyj.assets.mapper.AssetsInfoMapper;
import com.zyj.assets.domain.AssetsInfo;
import com.zyj.assets.service.IAssetsInfoService;

/**
 * 资产Service业务层处理
 * 
 * @author zhuyujie
 * @date 2022-05-09
 */
@Service
public class AssetsInfoServiceImpl implements IAssetsInfoService 
{
    @Autowired
    private AssetsInfoMapper assetsInfoMapper;

    @Autowired
    private AssetsReceiveMapper assetsReceiveMapper;

    /**
     * 查询资产
     * 
     * @param id 资产主键
     * @return 资产
     */
    @Override
    public AssetsInfo selectAssetsInfoById(String id)
    {
        AssetsReceive assetsReceive = new AssetsReceive ();
        assetsReceive.setAssetsId(id);
        List<AssetsReceive> assetsReceiveList = assetsReceiveMapper.selectAssetsReceiveList(assetsReceive);
        AssetsInfo assetsInfo = assetsInfoMapper.selectAssetsInfoById(id);
        assetsInfo.setAssetsReceiveList(assetsReceiveList);
        return assetsInfo;
    }

    /**
     * 查询资产列表
     * 
     * @param assetsInfo 资产
     * @return 资产
     */
    @Override
    public List<AssetsInfo> selectAssetsInfoList(AssetsInfo assetsInfo)
    {
        return assetsInfoMapper.selectAssetsInfoList(assetsInfo);
    }

    /**
     * 查询资产列表-非废弃
     *
     * @param assetsInfo 资产
     * @return 资产
     */
    @Override
    public List<AssetsInfo> selectAssetsInfoWithOutScrapList(AssetsInfo assetsInfo)
    {
        return assetsInfoMapper.selectAssetsInfoWithOutScrapList(assetsInfo);
    }

    /**
     * 新增资产
     * 
     * @param assetsInfo 资产
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAssetsInfo(AssetsInfo assetsInfo)
    {
        assetsInfo.setCreateTime(DateUtils.getNowDate());
        assetsInfo.setId(DateUtils.dateTimeNow());
        int rows = assetsInfoMapper.insertAssetsInfo(assetsInfo);
        insertAssetsReceive(assetsInfo);
        return rows;
    }

    /**
     * 修改资产
     * 
     * @param assetsInfo 资产
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAssetsInfo(AssetsInfo assetsInfo)
    {
//        assetsInfoMapper.deleteAssetsReceiveByAssetsId(assetsInfo.getId());
//        insertAssetsReceive(assetsInfo);
        return assetsInfoMapper.updateAssetsInfo(assetsInfo);
    }

    /**
     * 批量删除资产
     * 
     * @param ids 需要删除的资产主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAssetsInfoByIds(String ids)
    {
        assetsInfoMapper.deleteAssetsReceiveByAssetsIds(Convert.toStrArray(ids));
        return assetsInfoMapper.deleteAssetsInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除资产信息
     * 
     * @param id 资产主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAssetsInfoById(String id)
    {
        assetsInfoMapper.deleteAssetsReceiveByAssetsId(id);
        return assetsInfoMapper.deleteAssetsInfoById(id);
    }

    /**
     * 新增资产领用信息
     * 
     * @param assetsInfo 资产对象
     */
    public void insertAssetsReceive(AssetsInfo assetsInfo)
    {
        List<AssetsReceive> assetsReceiveList = assetsInfo.getAssetsReceiveList();
        String id = assetsInfo.getId();
        if (StringUtils.isNotNull(assetsReceiveList))
        {
            List<AssetsReceive> list = new ArrayList<AssetsReceive>();
            for (AssetsReceive assetsReceive : assetsReceiveList)
            {
                assetsReceive.setAssetsId(id);
                list.add(assetsReceive);
            }
            if (list.size() > 0)
            {
                assetsInfoMapper.batchAssetsReceive(list);
            }
        }
    }
}
